package com.lanou.musicbox.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.toolbox.ImageLoader;
import com.lanou.musicbox.R;
import com.lanou.musicbox.bean.RankingContent;
import com.lanou.musicbox.bean.RankingMusic;
import com.lanou.musicbox.utils.VolleySingleton;

import java.util.List;

/**
 * Created by dllo on 15/10/23.
 */
public class RankingMusicAdapter extends BaseAdapter {
    private List<RankingMusic.SongListEntity> songListEntities;
    private Context mContext;
    private ImageLoader imageLoader;

    public RankingMusicAdapter(List<RankingMusic.SongListEntity> datas, Context context) {
        this.songListEntities = datas;
        mContext = context;
        imageLoader = VolleySingleton.getInstance(mContext).getImageLoader();
        Log.i("lanou3344", "RankingMusicAdapter_songListEntities " + songListEntities.size());
    }

    @Override
    public int getCount() {
        return songListEntities != null && songListEntities.size() > 0 ? songListEntities.size() : 0;
    }

    @Override
    public Object getItem(int position) {
        return songListEntities != null && songListEntities.size() > 0 ? songListEntities.get(position) : 0;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View v, ViewGroup parent) {
        ViweHolder viweHolder;
        if (v == null) {
            viweHolder = new ViweHolder();
            //绑定布局
            v = LayoutInflater.from(mContext).inflate(R.layout.ranking_music_listview, null);
            viweHolder.ranking_album_title = (TextView) v.findViewById(R.id.ranking_album_title);
            viweHolder.ranking_author_name = (TextView) v.findViewById(R.id.ranking_author_name);
            viweHolder.Ranking_songs_img = (ImageView) v.findViewById(R.id.Ranking_songs_img);

            v.setTag(viweHolder);
        } else {
            viweHolder = (ViweHolder) v.getTag();
        }

        if (songListEntities != null) {
            viweHolder.ranking_author_name.setText(songListEntities.get(position).getAuthor());
            viweHolder.ranking_album_title.setText(songListEntities.get(position).getTitle());

            //设置图片
            ImageLoader.ImageListener imageListener = ImageLoader.getImageListener(
                    viweHolder.Ranking_songs_img, R.mipmap.ic_launcher, R.mipmap.ic_launcher);
            imageLoader.get(songListEntities.get(position).getPic_small(), imageListener);
        }

        return v;
    }

    public class ViweHolder {
        private ImageView Ranking_songs_img;
        private TextView ranking_album_title;
        private TextView ranking_author_name;


    }
}
